package com.zwj.service.impl;

import com.zwj.bo.GetCourseBo;
import com.zwj.bo.SelectTeamStudentsBo;
import com.zwj.dto.SelectTeamStudent;
import com.zwj.entity.*;
import com.zwj.enums.*;
import com.zwj.mapper.CommonMapper;
import com.zwj.service.CommonService;
import com.zwj.utils.DataCheckUtils;
import com.zwj.vo.EnumResult;
import com.zwj.vo.Result;
import com.zwj.vo.SelectTeacherVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommonServiceImpl implements CommonService {
    private final CommonMapper commonMapper;

    public CommonServiceImpl(CommonMapper commonMapper) {
        this.commonMapper = commonMapper;
    }
    @Override
    public Result enumValid() {
        return Result.success(ValidEnum.getEnumList());
    }

    @Override
    public Result getColleges() {
        List<CollegesEntity> colleges = commonMapper.getColleges();
        if(colleges.size()==0){
            return Result.error("院系查询结果为空");
        }
        return Result.success(colleges.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getCollegesName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getMajor() {
        List<MajorEntity> majorEntities = commonMapper.getMajor();
        if(majorEntities.size()==0){
            return Result.error("专业查询结果为空");
        }
        return Result.success(majorEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getMajorName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getYearInfo() {
        List<YearInfoEntity> yearInfoEntities = commonMapper.getYearInfo();
        if(yearInfoEntities.size()==0){
            return Result.error("年级查询结果为空");
        }
        return Result.success(yearInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getYearName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getClassBase() {
        List<ClassBaseInfoEntity> classBaseInfoEntities = commonMapper.getClassBase();
        if(classBaseInfoEntities.size()==0){
            return Result.error("班号查询结果为空");
        }
        return Result.success(classBaseInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getClassName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getTeacher() {
        List<UserEntity> userEntities = commonMapper.getTeacher();
        if(userEntities.size()==0){
            return Result.error("老师查询结果为空");
        }
        return Result.success(userEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getYesOrNo() {
        return Result.success(YesOrNoEnum.getEnumList());
    }

    @Override
    public Result getTeacherPosition() {
        return Result.success(TeacherPositionEnum.getEnumList());
    }

    @Override
    public Result getStudentPosition() {
        return Result.success(StudentPositionEnum.getEnumList());
    }

    @Override
    public Result getTeam() {
        List<TeamInfoEntity> teamInfoEntities = commonMapper.getTeam();
        if(teamInfoEntities.size()==0){
            return Result.error("项目组查询结果为空");
        }
        return Result.success(teamInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getTeamName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getCourse(GetCourseBo getCourseBo) {
        List<CourseInfoEntity> courseInfoEntities = commonMapper.getCourse(getCourseBo);
        if(courseInfoEntities.size()==0){
            return Result.error("课程查询结果为空");
        }
        return Result.success(courseInfoEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getCourseName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getTrainingTeacher() {
        List<SelectTeacherVo> TrainingTeachers = commonMapper.getCourseTeachers("");
        if(TrainingTeachers.size()==0){
            return Result.error("实训老师查询结果为空");
        }
        return Result.success(TrainingTeachers.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result getResponsibleTeacher() {
        List<SelectTeacherVo> TrainingTeachers = commonMapper.getCourseTeachers(TeacherRole.RESPONSIBLE.getKey());
        if(TrainingTeachers.size()==0){
            return Result.error("责任教师查询结果为空");
        }
        return Result.success(TrainingTeachers.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getName())
        ).collect(Collectors.toList()));
    }

    @Override
    public Result selectTeamStudents(SelectTeamStudentsBo selectTeamStudentsBo) {
        DataCheckUtils.isValid(selectTeamStudentsBo.getTeamInfoId().length!=0, "项目ID不能为空!");
        for(String teamInfoId: selectTeamStudentsBo.getTeamInfoId()){
            TeamInfoEntity teamInfoEntity =commonMapper.getTeamById(teamInfoId);
            if(teamInfoEntity==null){
                return Result.error("项目组ID"+teamInfoId+"不存在");
            }
            if(teamInfoEntity.getIsGraduation().equals(YesOrNoEnum.YES.getKey())){
                return Result.error("项目组ID"+teamInfoId+"已结业");
            }
        }
        String teamInfoIds = StringUtils.join(selectTeamStudentsBo.getTeamInfoId(),",");
        List<UserEntity> userEntities = commonMapper.selectTeamStudents(new SelectTeamStudent(teamInfoIds,""));
        if(userEntities.size()==0){
            return Result.error("项目组学生查询结果为空");
        }
        return Result.success(userEntities.stream().map(
                entry -> new EnumResult(entry.getId(), entry.getName())
        ).collect(Collectors.toList()));
    }
}
